package com.zmj.sy.mom.srv.aps.mapper;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zmj.sy.mom.srv.aps.bean.entity.third.SinoCamPushDrawing;
import com.zmj.sy.mom.srv.aps.bean.vo.order.OrderDrawPushStatusResVo;
import com.zmj.sy.mom.srv.aps.bean.vo.order.OrderPushStatusResVo;
import com.zmj.sy.mom.srv.aps.bean.vo.texturereplace.TextureReplaceSelectResVo;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.poi.ss.formula.functions.T;

import java.util.List;


@Mapper
public interface SinoCamPushDrawingMapper extends SyMapper<SinoCamPushDrawing> {

    @Select("delete from tb_third_sinocam_pushdrawing where sino_cam_id = #{pid}")
    void deleteByPid(@Param("pid") Integer pid);

    @Select("SELECT o.order_code AS orderCode, d.texture AS texture, d.thick_ness AS thickness, p.chart_id AS chartId, p.part_id AS partId, d.create_username AS createUsername, d.create_date_time AS createDateTime, p.replace_texture AS replaceTexture, p.processing_no AS packageCode, p.plan_amount AS totalCount, p.plan_finish_amount AS finishCount FROM tb_third_sinocam_pushdrawing d LEFT JOIN tb_order o ON d.order_id=o.id LEFT JOIN tb_third_sinocam_pushplan p ON d.uni_mater_code=p.uni_mater_code WHERE p.nest_plan_id=#{nestPlanId}")
    List<TextureReplaceSelectResVo> textureReplaceSelect(@Param("nestPlanId") String nestPlanId);

    @Select("( SELECT " +
            "  d.id AS id, " +
            "  o.serial_code AS orderCode, " +
            "  o.mater_name AS productName, " +
            "  p.processing_no AS packageCode, " +
            "  p.component_name AS assPartName, " +
            "  p.component_id AS assPartCode, " +
            "  d.part_id AS partCode, " +
            "  d.part_name AS partName, " +
            "  d.texture AS texture, " +
            "  d.thick_ness AS thickness, " +
            "  p.plan_amount AS quantity, " +
            "  d.create_date_time AS pushTime, " +
            "  p.`status` AS `status`, " +
            "  p.error_msg AS remark, " +
            "  p.nest_plan_id AS nestPlanId " +
            "  FROM " +
            "    tb_third_sinocam_pushdrawing d " +
            "    LEFT JOIN tb_order o ON d.order_id = o.id " +
            "    LEFT JOIN tb_third_sinocam_pushplan p ON d.uni_mater_code = p.uni_mater_code " +
            "  ${ew.customSqlSegment} " +
            "    AND d.`status` = 2 " +
            "  ) UNION ALL " +
            "  ( " +
            "  SELECT " +
            "    d.id AS id, " +
            "    o.serial_code AS orderCode, " +
            "    o.mater_name AS productName, " +
            "    p.processing_no AS packageCode, " +
            "    p.component_name AS assPartName, " +
            "    p.component_id AS assPartCode, " +
            "    d.part_id AS partCode, " +
            "    d.part_name AS partName, " +
            "    d.texture AS texture, " +
            "    d.thick_ness AS thickness, " +
            "    p.plan_amount AS quantity, " +
            "    d.create_date_time AS pushTime, " +
            "    p.`status` AS `status`, " +
            "    p.error_msg AS remark, " +
            "    p.nest_plan_id AS nestPlanId " +
            "  FROM " +
            "    tb_third_sinocam_pushdrawing d " +
            "    LEFT JOIN tb_order o ON d.order_id = o.id " +
            "    LEFT JOIN tb_third_sinocam_pushplan p ON d.uni_mater_code = p.uni_mater_code " +
            "  ${ew.customSqlSegment}  " +
            "    AND d.`status` <> 2  " +
            "  ORDER BY " +
            "    d.`status` DESC ) ")
    Page<OrderPushStatusResVo> pushStatusSelect(Page<T> page, @Param(Constants.WRAPPER) QueryWrapper<SinoCamPushDrawing> query);


    @Select("DELETE FROM tb_third_sinocam_pushdrawing WHERE order_id=#{orderId}")
    void deletePreOrder(@Param("orderId") Integer orderId);

    @Select("( SELECT " +
            "  o.serial_code AS orderCode, " +
            "  o.mater_name AS materName, " +
            "  d.uni_mater_code AS uniCode, " +
            "  o.push_type AS pushType, " +
            "  d.part_id AS partCode, " +
            "  d.part_name AS partName, " +
            "  d.texture AS texture, " +
            "  d.thick_ness AS thickness, " +
            "  d.create_date_time AS pushTime, " +
            "  d.`status` AS pushStatus, " +
            "  d.error_msg AS remark " +
            "  FROM " +
            "    tb_third_sinocam_pushdrawing d " +
            "    LEFT JOIN tb_order o ON d.order_id = o.id " +
            "  ${ew.customSqlSegment} " +
            "    AND d.`status` = 2 " +
            "  ) UNION ALL " +
            "  ( " +
            "  SELECT " +
            "    o.serial_code AS orderCode, " +
            "    o.mater_name AS materName, " +
            "    o.push_type AS pushType, " +
            "    d.uni_mater_code AS uniCode, " +
            "    d.part_id AS partCode, " +
            "    d.part_name AS partName, " +
            "    d.texture AS texture, " +
            "    d.thick_ness AS thickness, " +
            "    d.create_date_time AS pushTime, " +
            "    d.`status` AS pushStatus, " +
            "    d.error_msg AS remark " +
            "  FROM " +
            "    tb_third_sinocam_pushdrawing d " +
            "    LEFT JOIN tb_order o ON d.order_id = o.id " +
            "  ${ew.customSqlSegment}  " +
            "    AND d.`status` <> 2 " +
            "ORDER BY " +
            "  d.`status` DESC ) ")
    Page<OrderDrawPushStatusResVo> drawPushStatus(Page<T> page, @Param(Constants.WRAPPER) QueryWrapper<SinoCamPushDrawing> query);

}
